package org.xenon.vote.dao.imp;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.xenon.vote.dao.IntDao;
import org.xenon.vote.model.pojo.po.Ballot;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class BallotDao implements IntDao<Ballot> {
    private final JdbcTemplate jdbcTemplate;

    public BallotDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void add(Ballot ballot) {
        String sql = "insert into `vote`.`ballot` (`name`, `owner`, `description`, `time`, `birth`) values (?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, ballot.getName(), ballot.getOwner(), ballot.getDescription(), ballot.getTime(), ballot.getBirth());
    }

    @Override
    public void edit(Ballot ballot) {
        String sql = "update `vote`.`ballot` set `name` = ? , `owner` = ? , `description` = ? , `time` = ?, `birth` = ? where `id` = ?";
        jdbcTemplate.update(sql, ballot.getName(), ballot.getOwner(), ballot.getDescription(), ballot.getTime(), ballot.getBirth());
    }

    /**
     *
     * @param id 投票的ID
     */
    @Override
    public Ballot get(int id) {
        String sql = "select * from `vote`.`ballot` where `id` = ?";
        List<Ballot> list = jdbcTemplate.query(sql, new Object[]{id}, BallotRowMapper.getInstance());
        if (list.size() == 0) return null;
        return list.get(0);
    }

    @Deprecated
    @Override
    public List<Ballot> getAll(int id) {
        String sql = "select * from `vote`.`ballot` where `id` = ?";
        return jdbcTemplate.query(sql, new Object[]{id}, BallotRowMapper.getInstance());
    }

    public List<Ballot> getAll(String mail) {
        String sql = "select * from `vote`.`ballot` where owner = ? order by time desc ";
        return jdbcTemplate.query(sql, new Object[]{mail}, BallotRowMapper.getInstance());
    }

    /**
     * 关键字模糊查询
     * @param mail 邮件地址
     * @param keyword 关键字, 投票名字, 支持模糊查询
     */
    public List<Ballot> getAll(String mail, String keyword) {
        String sql = "select * from `vote`.`ballot` where owner = ? and name like ?";
        return jdbcTemplate.query(sql, new Object[]{mail, keyword}, BallotRowMapper.getInstance());
    }

    @Override
    public void del(Ballot ballot) {
        String sql = "delete FROM `vote`.`ballot` WHERE `id` = ?";
        jdbcTemplate.update(sql, ballot.getId());
    }

    @Override
    public void del(int id) {
        String sql = "delete FROM `vote`.`ballot` WHERE `id` = ?";
        jdbcTemplate.update(sql, id);
    }

    /**
     * 自动以RowMapper
     * 提高性能, 使用单例模式
     */
    private static class BallotRowMapper implements RowMapper<Ballot> {

        private static BallotRowMapper ballotRowMapper;

        @Override
        public Ballot mapRow(ResultSet rs, int i) throws SQLException {
            return new Ballot(rs.getInt("id"),
                    rs.getString("name"),
                    rs.getString("description"),
                    rs.getDate("time"),
                    rs.getDate("birth"),
                    rs.getString("owner"));
        }

        static BallotRowMapper getInstance() {
            if (ballotRowMapper == null) {
                synchronized (BallotRowMapper.class) {
                    if (ballotRowMapper == null) {
                        ballotRowMapper = new BallotRowMapper();
                    }
                }
            }
            return ballotRowMapper;
        }
    }
}
